package app.controller;

import app.book.Book;
import app.book.BookDao;
import app.book.Books;
import app.util.Filters;
import app.util.Path;
import app.util.Property;
import app.util.RequestUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import spark.ModelAndView;
import spark.Request;
import spark.Response;
import spark.Route;
import spark.template.mustache.MustacheTemplateEngine;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class IndexController {
	
	private final static List<Property> bookCategoryOptions = Filters.getBookCategoryOptions();
	private final static Logger logger = LoggerFactory.getLogger(IndexController.class);
	
    public static Route serveListPage = (Request req, Response resp) -> {
    	logger.info("request page: /list");
        Map<String, Object> model = new HashMap<>();
    	String dateSelected = RequestUtil.getParamDateSelected(req);
		model.put("dateselected", dateSelected);
		model.put("today", LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd")));
		model.put("categoryOptions", bookCategoryOptions);
        MustacheTemplateEngine engine = new MustacheTemplateEngine();
        ModelAndView mav = new ModelAndView(model, Path.Template.INDEX);
		return engine.render(mav);
    };
    
    public static Route serveAddPage = (Request req, Response resp) -> {
		String bookName = RequestUtil.getParamBookName(req);
		String bookCat = RequestUtil.getParamBookCategory(req);
    	String dateSelected = RequestUtil.getParamDateSelected(req);
    	logger.info(String.format("add book to %s: %s(%s)", dateSelected, bookName, bookCat));
		boolean ret = BookDao.addBookToDate(dateSelected, bookName, bookCat);
		if (ret) {
			logger.info("add book operation successed");
		} else {
			logger.warn("add book operation failed");
		}
		resp.type("application/json");
		return String.format("{\"ok\": %s}", ret);
    };
    
    public static Route serveDelPage = (Request req, Response resp) -> {
    	String bookId = RequestUtil.getParamBookID(req);
    	String dateSelected = RequestUtil.getParamDateSelected(req);
    	logger.info(String.format("delete book from %s: %s", dateSelected, bookId));
		boolean ret = BookDao.delBookById(dateSelected, bookId);
		if (ret) {
			logger.info("delete book operation successed");
		} else {
			logger.warn("delete book operation failed");
		}
		resp.type("application/json");
		return String.format("{\"ok\": %s}", ret);
    };
    
    public static Route serveBooksRequest = (Request req, Response resp) -> {
    	String dateSelected = RequestUtil.getParamDateSelected(req);
		logger.info(String.format("requesting book list at %s", dateSelected));
    	Books books = BookDao.getBooksByDate(dateSelected);
		logger.info(String.format("got %d books", books.getTotal()));
		for (Book b : books.getBooks()) {
			logger.info(String.format("\t%s: %s(%s) @ %s", b.getId(), b.getName(), b.getCategory(), b.getReadDate()));
		}
		resp.type("application/json");
		return books.toJsonString();
    };

    public static Route serveBookCountByDateRequest = (Request req, Response resp) -> {
		String date = RequestUtil.getParamDate(req);
		int count = BookDao.bookCountByDate(date);
		// logger.info(String.format("requesting book count at %s: %d", date, count));
		resp.type("application/json");
		return String.format("{\"count\": %d}", count);
	};
}
